package it.avantsoft.ls.simulation;

import org.apache.log4j.Logger;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class SimulationMatrix {
    private static Logger log = Logger.getLogger(SimulationMatrix.class);

    private int peerNumber = 0;
    private int peerAlive = 0;
    private ConcurrentHashMap<Integer, Map> simMatrix = null;


    public SimulationMatrix(int PeerNumber) {
        this.peerNumber = PeerNumber;
        simMatrix = new ConcurrentHashMap<Integer, Map>(PeerNumber);
        Random randGen = new Random();
        for (int i = 0; i < PeerNumber; i++) {
            ConcurrentHashMap<Integer, SimAppraisals> PeerHashMap = new ConcurrentHashMap<Integer, SimAppraisals>(PeerNumber);
            for (int j = 0; j < PeerNumber; j++) {

                float decimaleFloat = (randGen.nextFloat() * 100) % 129;
                int percentuale = randGen.nextInt(100);
                long tempo = Math.abs(randGen.nextLong());
                int slots = randGen.nextInt(10);
                PeerHashMap.put(j + 5010, new SimAppraisals(j + 5010, slots, System.currentTimeMillis()));


            }
            simMatrix.put(i + 5010, PeerHashMap);

        }
        /*   Random randGen = new Random();

        for (int i = 0; i < PeerNumber; i++) {

            for (int j = 0; j < PeerNumber; j++) {
                // valore compreso tra 0 e 10
                float decimaleFloat = (randGen.nextFloat()*100 )% 129;
                int percentuale = randGen.nextInt(100);
                long tempo = Math.abs(randGen.nextLong());
                int slots = randGen.nextInt(10);
                simMatrix[i][j] = new SimAppraisals(decimaleFloat, 0, 0, slots, 0);
            }

        }*/

    }

    public void newPeer(int clientId) {
        Random randGen = new Random();
        log.info("clientIDMatrix:" + clientId);
        int limit = getPeerNumber();
        for (int j = 0; j < limit; j++) {
            int i = limit - 1;
            // valore compreso tra 0 e 10
            float decimaleFloat = (randGen.nextFloat() * 100) % 129;
            int percentuale = randGen.nextInt(100);
            long tempo = Math.abs(randGen.nextLong());
            int slots = randGen.nextInt(10);
            //simMatrix[i][j] = new SimAppraisals(clientId, decimaleFloat, 0, 0, slots, 0);
        }
        for (int i = 0; i < limit; i++) {
            int j = limit - 1;
            // valore compreso tra 0 e 10
            float decimaleFloat = (randGen.nextFloat() * 100) % 129;
            int percentuale = randGen.nextInt(100);
            long tempo = Math.abs(randGen.nextLong());
            int slots = randGen.nextInt(10);
            // simMatrix[i][j] = new SimAppraisals(clientId, decimaleFloat, 0, 0, slots, 0);
        }
        this.peerNumber++;
        //log.info(simulationMatrix);
    }

    private int getPeerNumber() {
        return peerNumber;
    }

    public SimAppraisals crossPeers(String idPeerSender, String idPeerReceiver) {

        int sender = Integer.parseInt(idPeerSender);
        int receiver = Integer.parseInt(idPeerReceiver);
        return (SimAppraisals) ((ConcurrentHashMap) (simMatrix.get(sender))).get(receiver);
    }

    public SimAppraisals getPeerAppraisals(String PeerId) {

        return getPeerAppraisals(Integer.valueOf(PeerId));
    }

    public SimAppraisals getPeerAppraisals(int peerId) {
        //log.info("peerid "+peerId);
        return (SimAppraisals) ((ConcurrentHashMap) (simMatrix.get(peerId))).get(peerId);

    }

    public String toString() {
        return "SimulationMatrix{" +
                "simMatrix=" + simMatrix +
                '}';
    }
}
